package com.five.api.interceptors;

import com.alibaba.fastjson.JSON;
import com.five.util.MySessionContext;
import com.five.vo.BaseResultVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: star
 * @Date: 2021-05-25 14:55
 *  登录验证拦截器
 */
//public class LoginInterceptors implements HandlerInterceptor {

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
//        request.setCharacterEncoding("utf-8");
//
//        if(null == request.getRequestedSessionId()){
//            failResp(response,"用户还未登录");
//            return false;
//        }
//
//        //获取session对象
//        MySessionContext myc= MySessionContext.getInstance();
//        HttpSession session = myc.getSession(request.getRequestedSessionId());
//        if(null == session){
//            failResp(response,"用户登录已过期，请重新登录");
//            return false;
//        }
//
//        return true;
//    }
//
//    /**
//     * 返回异常信息
//     * @param message   异常信息
//     */
//    public static void failResp(HttpServletResponse response,String message){
//        BaseResultVO baseResult = new BaseResultVO();
//        baseResult.setCode(BaseResultVO.Code.NOT_LOGIN.getValue());
//        baseResult.setMessage(message);
//
//        String json = JSON.toJSONString(baseResult);
//        try {
//            response.setContentType("application/json;charset=utf-8");
//            response.getWriter().write(json);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//}
